package com.rfid.reader;

import com.rfid.spec.SpecInvenParamSet;

public class DriverPC8001 extends DriverPC301 {
    private int m_AntennaSelected;
    private byte[] defAntennaIDs;
    public DriverPC8001() {
        this.m_AntennaSelected = 0;
        this.defAntennaIDs = null;
        this.defaultInventoryTimeout = 30000;
        this.defAntennaIDs = new byte[GetAntennaInterfaceCount()];
        for (int i = 0; i < this.defAntennaIDs.length; i++) this.defAntennaIDs[i] = (byte) (i + 1);
    }
    public String GetDeviceName() {
        return "PC8001";
    }

    public int GetAntennaInterfaceCount() {
        return 30;
    }
    public int PICC_Inventory(byte AIType, byte[] AntennaIDs, int mTimeout, SpecInvenParamSet InvenParams) {
        int mInvenTimeout = this.defaultInventoryTimeout;
        if (mTimeout > 0) mInvenTimeout = mTimeout;
        if (AntennaIDs == null) AntennaIDs = this.defAntennaIDs;
        int iret = 0;
        if (AntennaIDs != null && AntennaIDs.length > 0) {
            int AntennaSelected = 0;
            for (int i = 0; i < AntennaIDs.length; i++)
                if (AntennaIDs[i] >= 1 && AntennaIDs[i] <= GetAntennaInterfaceCount())
                    AntennaSelected |= 1 << AntennaIDs[i] - 1;
            boolean AntennaConfigured = false;
            if (this.m_AntennaSelected == AntennaSelected) AntennaConfigured = true;
            if (!AntennaConfigured) {
                byte[] cfgblock = new byte[8];
                iret = ReadCfgBlock(1, cfgblock);
                if (iret != 0) return iret;
                cfgblock[1] = (byte) (AntennaSelected & 0xFF);
                cfgblock[2] = (byte) (AntennaSelected >> 8 & 0xFF);
                cfgblock[4] = (byte) (AntennaSelected >> 16 & 0xFF);
                cfgblock[5] = (byte) (AntennaSelected >> 24 & 0xFF);
                iret = WriteCfgBlock(1, cfgblock);
                if (iret != 0) return iret;
            }
            this.m_AntennaSelected = AntennaSelected;
        }
        return super.PICC_Inventory(AIType, AntennaIDs, mInvenTimeout, InvenParams);
    }

    public int SetAcessAntenna(int AntennaID) {
        this.m_syncLock.lock();
        this.pbSnd.flush();
        this.pbRecv.flush();
        char cmd = 1190;
        this.pbSnd.WriteData(cmd);
        this.pbSnd.WriteData((byte) (AntennaID & 0xFF));
        int iret = transceive(cmd, this.pbSnd, this.pbRecv, 500L, false);
        if (iret != 0) {
            this.m_syncLock.unlock();
            return iret;
        }
        this.m_syncLock.unlock();
        iret = 0;
        return iret;

    }



}
